﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using qlqa.Models.LinQ;

namespace qlqa.Delegate.FoodDelegate
{
    public class FoodDelegate
    {
        DatabaseDataContext _database = new DatabaseDataContext();
        #region Quản lý món ăn

        /// <summary>
        ///     Lấy món ăn theo loại
        /// </summary>
        /// <param name="timKiem"></param>
        /// <param name="type">Loại</param>
        /// <returns></returns>
        public List<tb_MonAn> GetFoodByType(string timKiem,int? type)
        {
            try
            {
                if (type == 0 || type==null)
                {
                    if (string.IsNullOrEmpty(timKiem))
                    {
                        return (from menu in _database.tb_ThucDons
                                from food in _database.tb_MonAns
                                where
                                    menu.Id == food.IdThucDon && (menu.IsDelete == false || menu.IsDelete == null) &&
                                    menu.IsSuDung == true
                                    && (food.IsDelete == false || food.IsDelete == null)
                                select food).ToList();
                    }
                    else
                    {
                        return (from menu in _database.tb_ThucDons
                                from food in _database.tb_MonAns
                                where
                                    menu.Id == food.IdThucDon && (menu.IsDelete == false || menu.IsDelete == null) &&
                                    menu.IsSuDung == true
                                    && (food.IsDelete == false || food.IsDelete == null) && (food.Ten.Contains(timKiem))
                                select food).ToList();
                    }
                }
                if (string.IsNullOrEmpty(timKiem))
                {
                    return
                        _database.tb_MonAns.Where(
                            p => p.IdThucDon == type && (p.IsDelete == false || p.IsDelete == null))
                            .ToList();
                }
                else
                {
                    return
                       _database.tb_MonAns.Where(
                           p => p.IdThucDon == type && (p.IsDelete == false || p.IsDelete == null) && p.Ten.Contains(timKiem))
                           .ToList();
                }
            }
            catch (Exception)
            {
                return new List<tb_MonAn>();
            }
        }

        public tb_MonAn GetFoodById(int id)
        {
            try
            {
                return _database.tb_MonAns.FirstOrDefault(p => p.Id == id);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool UpdateFood(tb_MonAn obj)
        {
            try
            {
                var food = _database.tb_MonAns.FirstOrDefault(p => p.Id == obj.Id);
                food.Ten = obj.Ten;
                if (!string.IsNullOrEmpty(obj.Hinh))
                {
                    food.Hinh = obj.Hinh;
                }              
                food.Gia = obj.Gia;
                food.NgaySua = DateTime.Now;
                food.IsSuDung = obj.IsSuDung;
                food.NguoiSua = Convert.ToInt32(HttpContext.Current.Session["USERID"] ?? 0);
                _database.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool InsertFood(tb_MonAn obj)
        {
            try
            {
                obj.NguoiTao = Convert.ToInt32(HttpContext.Current.Session["USERID"] ?? 0);
                obj.NgayTao = DateTime.Now;
                _database.tb_MonAns.InsertOnSubmit(obj);
                _database.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DeleteFood(int idMonAn)
        {
            try
            {
                var food = _database.tb_MonAns.FirstOrDefault(p => p.Id == idMonAn);
                food.NguoiSua = Convert.ToInt32(HttpContext.Current.Session["USERID"] ?? 0);
                food.NgaySua = DateTime.Now;
                food.IsDelete = true;
                _database.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        #endregion
    }
}